package com.flyme.module.base.provider.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.module.base.client.api.SysEducationRemoteService;
import com.flyme.module.base.client.api.SysWorkCourseRemoteService;
import com.flyme.module.base.client.entity.SysEducation;
import com.flyme.module.base.client.entity.SysWorkCourse;
import com.flyme.module.base.client.entity.Sysenum;
import com.flyme.module.base.client.enums.EducationEnum;
import com.flyme.module.base.client.enums.ProfessionEnum;
import com.flyme.module.base.client.enums.RankEnum;
import com.flyme.module.base.provider.service.*;
import com.flyme.module.exam.client.entity.BusUserability;
import com.flyme.module.exam.client.entity.ExamType;
import com.flyme.module.exam.client.enums.AbilityItemEnum;
import com.flyme.module.exam.provider.service.BusUserabilityService;
import com.flyme.module.exam.provider.service.ExamTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * 教育经历
 *
 * @author zyf
 */
@Api(tags = "教育经历控制器")
@RestController
public class SysEducationController extends BaseController<SysEducationService, SysEducation> implements SysEducationRemoteService {

    @Autowired
    private SysConfigService configService;
    @Autowired
    private SysenumService sysenumService;
    @Autowired
    private ExamTypeService examTypeService;
    @Autowired
    private BusUserabilityService userabilityService;

    /**
     * 查询用户教育经历
     *
     * @return
     */
    @ApiOperation(value = "查询用户教育经历")
    @Override
    public ResultBody selectByUserId(@PathVariable Long userId) {
        List<SysEducation> list = bizService.selectByUserId(userId);
        for (SysEducation education : list) {
            education.setEducation(EducationEnum.getText(education.getEducation()));
            education.setMajor(ProfessionEnum.getText(education.getMajor()));
            education.setGrade(RankEnum.getText(education.getGrade()));
        }
        return ResultBody.success(list);
    }

    /**
     * 认证用户教育经历
     */
    @ApiOperation(value = "认证用户教育经历")
    public ResultBody checkUserEdu(@PathVariable String id) {
        UpdateWrapper u = new UpdateWrapper();
        boolean a = bizService.update(u.set(true, "isEducation", 1).eq(true, "id", id));
        if(a){
            gradeEducation(bizService.getById(id));
        }
        return ResultBody.success();
    }


    /**
     * 认证用户教育经历
     */
    @ApiOperation(value = "拒绝认证用户教育经历")
    public ResultBody checkUserEduRefuse(@PathVariable String id) {
        UpdateWrapper u = new UpdateWrapper();
        boolean a = bizService.update(u.set(true, "isEducation", 3).eq(true, "id", id));
        return ResultBody.success();
    }

    /**
     * 教育经历打分
     */
    private void gradeEducation(SysEducation education) {
        BigDecimal basics = new BigDecimal(configService.getByName("EDUCATION").getConfigVal());//基础分
        Sysenum educationEnum = sysenumService.findByCode(education.getEducation());//学历
        BigDecimal a = new BigDecimal(educationEnum.getScore());
        Sysenum majorEnum = sysenumService.findByCode(education.getMajor());//专业
        BigDecimal b = new BigDecimal(majorEnum.getScore());
        BigDecimal score = basics.multiply(a).multiply(b);
        ExamType abilityItem = examTypeService.findByCode(AbilityItemEnum.education.code);
        BusUserability userability = userabilityService.findUser(education.getUserId(), abilityItem.getId());
        if (ObjectUtils.isNotEmpty(userability)) {
            userability.setAbilityScore(String.valueOf(score));
            userabilityService.saveOrUpdate(userability);
        }
    }


}
